package boundary;

import java.awt.GridBagLayout;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Dimension;
import java.awt.Cursor;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;

public class NuovaRicetta extends JPanel implements ActionListener{

	private static final long serialVersionUID = 1L;
	private Home home;
	private String nomeR;
	private JLabel titleLabel = null;
	private JLabel jLabel = null;
	private JTextField jTextField = null;
	private JButton jButton = null;

	/**
	 * This is the default constructor
	 */
	public NuovaRicetta(Home home) {
		super();
		this.home=home;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
		gridBagConstraints3.gridx = 0;
		gridBagConstraints3.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints3.gridwidth = 2;
		gridBagConstraints3.gridy = 2;
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints2.gridy = 1;
		gridBagConstraints2.weightx = 1.0;
		gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints2.gridx = 1;
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.gridx = 0;
		gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints1.gridy = 1;
		jLabel = new JLabel();
		jLabel.setText("Nome");
		jLabel.setFont(new Font("Dialog", Font.BOLD, 12));
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.insets = new Insets(5, 5, 5, 5);
		gridBagConstraints.gridy = 0;
		titleLabel = new JLabel();
		titleLabel.setText("Inserisci nuova ricetta");
		titleLabel.setFont(new Font("Dialog", Font.BOLD, 20));
		this.setSize(297, 112);
		this.setLayout(new GridBagLayout());
		this.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		this.add(titleLabel, gridBagConstraints);
		this.add(jLabel, gridBagConstraints1);
		this.add(getJTextField(), gridBagConstraints2);
		this.add(getJButton(), gridBagConstraints3);
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setColumns(20);
			jTextField.setFont(new Font("Dialog", Font.PLAIN, 12));
			jTextField.setActionCommand("buttonPressed");
			jTextField.addKeyListener(new KeyAdapter() {
				public void keyReleased(KeyEvent k) {
					if (jTextField.getText().length() == 0)
						getJButton().setEnabled(false);
					else if (!getJButton().isEnabled())
						getJButton().setEnabled(true);
				}
			});
			jTextField.addActionListener(this);
		}
		return jTextField;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Inserisci");
			jButton.setEnabled(false);
			jButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			jButton.setFont(new Font("Dialog", Font.BOLD, 12));
			jButton.setIcon(new ImageIcon(getClass().getResource("/icone/notes-edit-icon.png")));
			jButton.addActionListener(this);
		}
		return jButton;
	}
	
	public void focus() {
		getJTextField().requestFocus();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		nomeR=getJTextField().getText();
		if (nomeR.length() > 1)
			home.inserireRicetta(nomeR);
	}

}  //  @jve:decl-index=0:visual-constraint="0,0"
